<?php
namespace Home\Controller;

class PrintSettingController extends ParentController
{
    protected function _initialize()
    {
        $this->validateUtil = new \Org\Util\Validate();
        $hotel = I('request.hotel');
        // 检查客栈是否已过期
        $hotelLogic = A('Common/Hotel', 'Logic');
        $result = $hotelLogic->checkHotelActive($hotel);
        if ($result == false) {
            returnJson(403, 408);
        }
        // 检查客栈状态
        $result = $hotelLogic->checkHotelStatus($hotel);
        if ($result == false) {
            returnJson(403, 401);
        }
        //检查权限
        $adminAuthorityLogic = A('Common/AdminAuthority', 'Logic');        
        $adminAuthorityLogic->check($hotel);
    }
    
    public function getSetting()
    {
        $param = I('post.');
        
        $validateRule = array(
            'hotel' => 'notnull,notempty,nameLength20',
            'printType' => 'notnull,notempty,id'
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        
        $hotelId = $param['hotel'];
        $printType = $param['printType'];
        $printSettingModel = D('PrintSetting');
        $printSetting = $printSettingModel->getSetting($hotelId, $printType);
        if (! empty($printSetting) && is_array($printSetting))
        {            
            unset($printSetting['id']);
            unset($printSetting['hotelentity_id']);
            unset($printSetting['printtype']);
        }else 
        {
            $data['hotelentity_id'] = $hotelId;
            $data['printtype'] = $printType;
            $data['printerpaper'] = $printType == 1 ? 'A4' : 'X58';
            $data['tips1'] = I('cookie.printSettingHint1');
            $data['tips2'] = I('cookie.printSettingHint2');
            $printSettingModel->data($data)->add();
            $printSetting = $printSettingModel->getSetting($hotelId, $printType);
            unset($printSetting['id']);
            unset($printSetting['hotelentity_id']);
            unset($printSetting['printtype']);
        }

        $hotelEntityModel = D('HotelEntity');
        $hotelInfo = $hotelEntityModel->getInfo($hotelId);
        if(!empty($hotelInfo)){
            $printSetting['hotelName'] = $hotelInfo['name'];
        }else {
            $printSetting['hotelName'] = '';
        }
        
        $printSetting['printTime'] = date('Y-m-d H:i:s');

        returnJson(200, 200, $printSetting);
    }

    public function saveOrderSetting()
    {
        $this->saveSetting();
    }
    public function saveQrcodeSetting()
    {
        $this->saveSetting();
    }
    protected function saveSetting()
    {
        $param = I('post.');
        
        $validateRule = array(
            'hotel' => 'notnull,notempty,id',
            'printType' => 'notnull,notempty,id',
            'preview' => 'booleanInt',
            'printtimes' => 'id',
            'printername' => 'nameLength50',
            'printerpaper' => 'nameLength20',
            'tips1' => 'nameLength50',
            'tips2' => 'nameLength50',
            'tips3' => 'nameLength50',
            'printmobile' => 'booleanInt',
            'printidentity' => 'booleanInt',
            'checkoutprint' => 'booleanInt'
        );
        $this->ajaxValidateReturn($this->validateUtil, $param, $validateRule);
        
        $hotelId = $param['hotel'];
        $printType = $param['printType'];
        
        if (! in_array($printType, array(1,2)))
        {
            returnJson(500, 503);
        }
        
        if (isset($param['preview'])) {
            $data['preview'] = $param['preview'];
        }
        if (isset($param['printtimes'])) {
            $data['printtimes'] = $param['printtimes'];
        }
        if (isset($param['printername'])) {
            $data['printername'] = $param['printername'];
        }
        if (! empty($param['printerpaper'])) {
            $data['printerpaper'] = $param['printerpaper'];
        }
        if (isset($param['tips1'])) {
            $data['tips1'] = $param['tips1'];
        }
        if (isset($param['tips2'])) {
            $data['tips2'] = $param['tips2'];
        }
        if (isset($param['tips3'])) {
            $data['tips3'] = $param['tips3'];
        }
        if (isset($param['printmobile'])) {
            $data['printmobile'] = $param['printmobile'];
        }
        if (isset($param['printidentity'])) {
            $data['printidentity'] = $param['printidentity'];
        }
        if (isset($param['checkoutprint'])) {
            $data['checkoutprint'] = $param['checkoutprint'];
        }
        
        $printSettingModel = D('PrintSetting');
        $printSetting = $printSettingModel->getSetting($hotelId, $printType);
        if (! empty($printSetting) && is_array($printSetting)) 
        {
            $option['hotelentity_id'] = $hotelId;
            $option['printtype'] = $printType;
            $printSettingModel->where($option)->save($data);
        }
        else 
        {
            if (empty($param['printerpaper'])) 
            {                
                $data['printerpaper'] = $printType == 1 ? 'A4' : 'X58';
            }
            $data['hotelentity_id'] = $hotelId;
            $data['printtype'] = $printType;
            $printSettingModel->data($data)->add();
        }
        
        returnJson(200, 200, 'success');
    }
}
